import Vue from 'vue';
import VueRouter from 'vue-router';
import PATH from "./constant"

Vue.use(VueRouter)

const routes = [
  {
    path: PATH.HOME,
    name: PATH.HEMES,
    component: PATH.HOMEASS,
    meta:PATH.HOMEMETA
  },
  {
    path: PATH.CLASSIFY,
    name: PATH.CLASSIFYS,
    component:PATH.CLASSIFYASS,
    meta:PATH.CLASSIFYMETA
  },
  {
    path: PATH.CART,
    name: PATH.CARTS,
    component:PATH.CARTASS,
    meta:PATH.CARTMETA,
    beforeEnter:PATH.CARTUSER
  },
  {
    path: PATH.USER,
    name: PATH.USERS,
    component:PATH.USERASS,
    meta:PATH.USERMETA
  },
  {
    path: PATH.SIGN,
    name: PATH.SIGNS,
    component:PATH.SIGNASS
  },
  {
    path: PATH.GIFT,
    name: PATH.GIFTS,
    component:PATH.GIFTASS
  },
  {
    path: PATH.SPECIAL,
    name: PATH.SPECIALS,
    component:PATH.SPECIALASS
  },
  {
    path: PATH.BARGAINING,
    name: PATH.BARGAININGS,
    component:PATH.BARGAININGASS,
    meta:PATH.BARGAININGMETA
  },
  {
    path:PATH.PAPULARBAR,
    name:PATH.POPULARBARS,
    component:PATH.PAPULARBARASS
  },
  {
    path:PATH.SELETED,
    name:PATH.SELETEDS,
    component:PATH.SELETEDASS
  },
  {
    path:PATH.CLASSIFYDETAIL,
    name:PATH.CLASSIFYDETAILS,
    component:PATH.CLASSIFYDETAILASS
  },
  {
    path:PATH.LOGIN,
    name:PATH.LOGINS,
    component:PATH.LOGINASS
  },
  {
    path:PATH.RESIGN,
    name:PATH.RESIGNS,
    component:PATH.RESIGNASS
  },
  {
    path:"/address",
    name:"address",
    component:()=>import("../views/address/Address"),
    meta:{
      title:"我的地址",
      right:"新建地址"
    }
  },
  {
    path:"/newaddress",
    name:"newaddress",
    component:()=>import("../views/address/NewAddress"),
    meta:{
      title:"新增地址",
    }
  },
  {
    path:"/placeOrder",
    name:"placeOrder",
    component:()=>import("../views/PlaceOrder"),
    meta:{
      title:"提交订单",
    },
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

import MANA from "../storage/constant";

/**
 * 由一个函数转换成全局方法
 * @param {*} path 
 */
router.goNext = (path)=>{
  router.push(path)
}
router.goBack = ()=>{
  router.back()
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
}

// router.beforeEach((to,from,next)=>{
//   if(localStorage.getItem(MANA.TOKEN)){
//     next()
//   }else{
//     if(to.name == PATH.LOGINS){
//       next()
//     }else{
//       next(PATH.LOGIN)
//     }
//   }
// })

export default router
